package nb.diskfilesystem.model;

import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;
import javafx.scene.layout.TilePane;
import nb.diskfilesystem.ui.FileItem;
import nb.diskfilesystem.ui.MainView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class DirectoryTree {
    private TreeView<String> treeView;
    private TreeItem<String> rootItem;
    private Map<String, List<DirectoryEntry>> folderContents; // 存储文件夹内容

    public DirectoryTree(TreeView<String> treeView) {
        this.treeView = treeView;
        this.rootItem = new TreeItem<>("C盘");
        this.treeView.setRoot(rootItem);
        this.treeView.setShowRoot(true); // 显示根节点

        // 初始化文件夹内容存储
        folderContents = new HashMap<>();
        folderContents.put("C盘", new ArrayList<>());

        // 添加双击事件处理器
        treeView.setOnMouseClicked(event -> {
            if (event.getClickCount() == 2) {
                TreeItem<String> selectedItem = treeView.getSelectionModel().getSelectedItem();
                if (selectedItem != null) {
                    String folderName = selectedItem.getValue();
                    // 更新 ScrollPane 的内容以展示该文件夹下的内容
                    updateScrollPaneContent(folderName);
                }
            }
        });

    }

    // 更新 ScrollPane 的内容
    private void updateScrollPaneContent(String folderName) {
        // 这里可以根据folderName加载对应文件夹的内容并更新ScrollPane
        // 清空当前内容，加载新FileItem
        TilePane fileArea = (TilePane) MainView.scrollPane.getContent();
        fileArea.getChildren().clear();

        // 获取文件夹的内容
        List<DirectoryEntry> entries = folderContents.get(folderName);
        if (entries != null && !entries.isEmpty()) {
            // 如果文件夹有内容，则加载内容
            for (DirectoryEntry entry : entries) {
                FileItem fileItem = new FileItem(entry,
                        entry.getType().equals("file") ? "/nb/diskfilesystem/css/file.css" : "/nb/diskfilesystem/css/folder.css",
                        entry.getName(), null, fileArea);
                fileArea.getChildren().add(fileItem);
            }
        } else {
            // 如果文件夹为空，则显示空白
            System.out.println("文件夹 " + folderName + " 为空");
        }
    }

    // 添加文件或文件夹到指定文件夹
    public void addItem(String parentFolder, DirectoryEntry entry) {
        TreeItem<String> parentItem = findItem(rootItem, parentFolder);
        if (parentItem != null) {
            parentItem.getChildren().add(new TreeItem<>(entry.getName()));

            // 更新文件夹内容
            folderContents.computeIfAbsent(parentFolder, k -> new ArrayList<>()).add(entry);
        }
    }

    // 查找路径对应的TreeItem
    private TreeItem<String> findItem(TreeItem<String> currentItem, String path) {
        if (currentItem.getValue().equals(path)) {
            return currentItem;
        }
        for (TreeItem<String> child : currentItem.getChildren()) {
            TreeItem<String> found = findItem(child, path);
            if (found != null) {
                return found;
            }
        }
        return null;
    }

    // 初始化目录树
    public void initializeTree() {
        addItem("C盘", new DirectoryEntry("测试目录1", "folder", "txt", 0, 0));
        addItem("C盘", new DirectoryEntry("测试目录2", "folder", "txt", 0, 0));
        addItem("C盘", new DirectoryEntry("测试目录3", "folder", "txt", 0, 0));
    }

    // 获取当前选中的TreeItem
    public TreeItem<String> getSelectedItem() {
        return treeView.getSelectionModel().getSelectedItem();
    }
}